package com.example.httpclient;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

public class SystemUtils {
    public static void getPermissions(Context context) {
        String[] permissions = {
                Manifest.permission.INTERNET,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS};
        ArrayList<String> mPermissionList = new ArrayList<>();
        //逐个判断你要的权限是否已经通过
        mPermissionList.clear();
        for (int i = 0; i < permissions.length; i++)
            if (ContextCompat.checkSelfPermission(context, permissions[i]) != PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);//添加还未授予的权限
            }
        if (mPermissionList.size() > 0) {//有权限没有通过，需要申请
            ActivityCompat.requestPermissions((Activity) context, permissions, 1111);

        } else{
            ActivityCompat.requestPermissions((Activity) context, permissions, 0000);
        }

    }

    public static void Exit(Context context) {
        try {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.AppTask> appTaskList = null;
                appTaskList = activityManager.getAppTasks();
                for (ActivityManager.AppTask appTask : appTaskList) {
                    appTask.finishAndRemoveTask();
                }
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        } catch (SecurityException e) {
            Log.i("Exception-Security", e.getMessage());
        }
        System.exit(0);

    }
}
